/*
 * @Description:
 * @Author: dh
 * @Date: 2021-09-24 14:38:26
 * @LastEditors: dh
 * @LastEditTime: 2024-05-14 14:51:13
 */

// TODO interface
interface Person1 {
	name: string
}
interface Person1 {
	age: number
}
// 同名 interface 会合并
let SemLinker: Person1 = {
	name: 'SemLinker',
	age: 33,
}

// 字面量类型 约束变量只能赋值 "男" 或 "女"
let gender: '男' | '女'
gender = '男'
gender = '女'

// 函数约束
interface mySearch {
	(source: string, subString: string): boolean
}
let mySearchFunc1: mySearch = function (source, subString) {
	let result = source.search(subString)
	return result > -1
}
// 推荐写法
function mySearchFunc2(source: string, subString: string): boolean {
	let result = source.search(subString)
	return result > -1
}

// Array 约束
interface StringArray1 {
	[i: number]: string
}

let myArray1: StringArray1 = ['Bob', 'Fred']
let myArray2: string[] = ['Bob', 'Fred']
let myArray3: Array<string> = ['Bob', 'Fred']
let myArray4: Array<string> | string[] = ['Bob', 'Fred']

// Class 约束, constructor 存在于类的静态部分，所以不会检查
interface ClockInterface {
	currentTime: Date
	setTime(d: Date): Date
}

class Clock implements ClockInterface {
	currentTime = new Date()
	setTime(d: Date) {
		this.currentTime = d
		return d
	}
	constructor(h: any, m: any) {
		// this.h = h;
		// this.m = m;
		// this.currentTime = currentTime;
	}
}

// TODO type 别名
type Str = string

// 字面量
type T = 'left' | 'right' | boolean

// 函数别名
type Cb = () => void
const cb: Cb = function () {}

// TODO 任意字段
interface Test2 {
	name: string
	readonly age?: number //不能修改
	[propName: string]: any
}
var test2: Test2 = {
	name: '1',
	test1: 1,
}
